package qeb.handheldmedical.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import qeb.handheldmedical.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2016年10月20日 下午3:59:36
 * @declaration
 */
public class ApprovalOperationAdapter extends BaseExpandableListAdapter {

	private int mCount;
	private Context mContext;
	private JSONArray jsonArray = null;

	public ApprovalOperationAdapter(Context context, JSONArray jsonArray) {
		this.mCount = jsonArray.length();
		this.mContext = context;
		this.jsonArray = jsonArray;
	}

	@Override
	public JSONObject getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		JSONObject tzd = null;
		try {
			tzd = jsonArray.getJSONObject(groupPosition);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tzd;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ItemHolder itemHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_approval_operation_patients, null);
			itemHolder = new ItemHolder();

			itemHolder.tvsqsj = (TextView) convertView
					.findViewById(R.id.tvsqsj);
			itemHolder.tvsssj = (TextView) convertView
					.findViewById(R.id.tvsssj);
			itemHolder.tvssmc = (TextView) convertView
					.findViewById(R.id.tvssmc);
			itemHolder.tvssfj = (TextView) convertView
					.findViewById(R.id.tvssfj);
			itemHolder.tvssth = (TextView) convertView
					.findViewById(R.id.tvssth);
			itemHolder.tvssys = (TextView) convertView
					.findViewById(R.id.tvssys);
			itemHolder.tvshys = (TextView) convertView
					.findViewById(R.id.tvshys);
			itemHolder.tvssjb = (TextView) convertView
					.findViewById(R.id.tvssjb);
			itemHolder.tvfzfs = (TextView) convertView
					.findViewById(R.id.tvfzfs);
			itemHolder.tvsszt = (TextView) convertView
					.findViewById(R.id.tvsszt);
			itemHolder.tvsslb = (TextView) convertView
					.findViewById(R.id.tvsslb);

			convertView.setTag(itemHolder);
		} else {
			itemHolder = (ItemHolder) convertView.getTag();
		}

		JSONObject patient = getChild(groupPosition, childPosition);
		try {
			itemHolder.tvsqsj.setText("申请时间：" + patient.getString("KDSJ"));
			itemHolder.tvsssj.setText("手术时间：" + patient.getString("SSSJ"));
			itemHolder.tvssmc.setText("手术名称：" + patient.getString("SSNAME"));
			itemHolder.tvssfj.setText("手术房间：" + patient.getString("SSFJ"));
			itemHolder.tvssth.setText("手术台号：" + patient.getString("SSTH"));
			itemHolder.tvssys.setText("主刀医生：" + patient.getString("ZDYS"));
			itemHolder.tvshys.setText("审核医生：" + patient.getString("KSSPRNAME"));
			itemHolder.tvssjb.setText("手术级别：" + patient.getString("SSDJ"));
			itemHolder.tvfzfs.setText("麻醉方式：" + patient.getString("MZFS"));
			itemHolder.tvsszt.setText("手术状态：" + patient.getString("SSZT"));
			itemHolder.tvsslb.setText("手术类别：" + patient.getString("SSLB"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public JSONObject getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		JSONObject tzd = new JSONObject();
		try {
			tzd = jsonArray.getJSONObject(groupPosition);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tzd;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return jsonArray.length();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		GroupHolder groupHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.approval_operation_group, null);

			groupHolder = new GroupHolder();
			groupHolder.tvch = (TextView) convertView.findViewById(R.id.tvch);
			groupHolder.tvsssj = (TextView) convertView
					.findViewById(R.id.tvssj);
			groupHolder.tvxm = (TextView) convertView.findViewById(R.id.tvxm);
			groupHolder.tvth = (TextView) convertView.findViewById(R.id.tvth);
			
			convertView.setTag(groupHolder);
		} else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		JSONObject tzd = getGroup(groupPosition);
		try {
			groupHolder.tvch.setText(tzd.getString("CH") + "床");
			
			try {
				String HHmm = tzd.getString("SSSJ").substring(
						tzd.getString("SSSJ").length() - 8,
						tzd.getString("SSSJ").length() - 3);
				groupHolder.tvsssj.setText("(" + HHmm + ")");
			} catch (Exception ex) {
			}
			groupHolder.tvxm.setText(tzd.getString("XM"));
			groupHolder.tvth.setText("台号：" + tzd.getString("SSTH") + "号");

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	class GroupHolder {
		public TextView tvch;
		public TextView tvxm;
		public TextView tvth;
		public TextView tvsssj;
	}

	class ItemHolder {
		TextView tvsqsj;
		TextView tvsssj;
		TextView tvssmc;
		TextView tvssfj;
		TextView tvssth;
		TextView tvssys;
		TextView tvshys;
		TextView tvssjb;
		TextView tvfzfs;
		TextView tvsszt;
		TextView tvsslb;
	}
}
